package com.smyh.protcol.host;

import com.smyh.util.AddCrc;
import com.smyh.util.DateUtil101V2;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.Date;

/**
 * @author Administrator
 * 
 */
public class HostCMDUtil {
	//TODO 链路地址长度  默认2
	static Integer addrLength = 2;
	//TODO 链路地址  默认0001
	static String linkAddrStr = "0001";
	//TODO 传输原因长度  默认1
	static Integer transCausesLength = 1;
	//TODO 信息体长度 默认2
	static Integer infoObjLength = 2;
	/**
	 * 获取初始化设备指令Iobuffer
	 * 
	 * @return 101 已修改
	 */
	public static IoBuffer initHostCMD() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		byte[] initContent = new byte[addrLength];
		for (int i = 0; i < addrLength; i++) {
			initContent[i] = (byte) 0xFF;
		}
		buf0.put(Protocol101Struts.INIT_HOST_01).put(initContent).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.FIX_FRAME_START).put(buf0).put(crcSum).put(
				Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 * 101 参数读取
	 * 
	 * @param type
	 * @return 已修改
	 */
	public static IoBuffer readParam(int type) {
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		byte controlCode = 0x53;
		// TODO 判断建立连接，初始化
		/*if (ContentMainUI_cache.connectButton.getText().equals("建立连接")) {
			UIElement.showMessageDialog("没有连接外设！");
			return null;
		}
		if (!HostInfoState.isReady()) {
			UIElement.showMessageDialog("设备没有初始化！");
			return null;
		}*/
		byte[] addrBytes = HexStrUtil.hexStr2Bytes(linkAddrStr);
		// 校验长度，返回复合properties文件长度大小的byte[]
		addrBytes = checkLength(addrBytes, addrLength, true);

		byte dataType = Protocol101Struts.DATA_TYPE_PARAM_SELF_CMD;
		byte vsqB = 0x1;
		byte[] transCauses = new byte[transCausesLength];
		byte transCause = 0x12;
		transCauses[0] = transCause;

		byte[] infoObjAddr = null;
		switch (type) {
		case 1: {
			infoObjAddr = Protocol101Struts.protocolParamAddr;
			break;
		}

		case 2: {
			infoObjAddr = Protocol101Struts.hardParamAddr;
			break;
		}

		case 3: {
			infoObjAddr = Protocol101Struts.grpsParamAddr;
			break;
		}

		case 4: {
			infoObjAddr = Protocol101Struts.softVersionParamAdrr;
			break;
		}

		case 5: {
			// 点位信息查询，再确认点位类型：遥信1-遥测2-遥调3-遥控4
			//TODO 判断读取点位类型
			int pointType = 1;
			switch (pointType) {
			case 1: {
				infoObjAddr = Protocol101Struts.pointDigit;
				break;
			}

			case 2: {
				infoObjAddr = Protocol101Struts.pointMock;
				break;
			}

			case 3: {
				infoObjAddr = Protocol101Struts.pointSet;
				break;
			}

			case 4: {
				infoObjAddr = Protocol101Struts.pointControl;
				break;
			}

			default:
				break;
			}
			break;
		}

		default:
			break;
		}
		// 校验长度，返回复合properties文件长度大小的byte[]
		infoObjAddr = checkLength(infoObjAddr, infoObjLength, true);

		buf0.put(controlCode).put(addrBytes).put(dataType).put(vsqB).put(
				transCauses).put(addrBytes).put(infoObjAddr).flip();
		byte crcSum = AddCrc.getCrc(buf0);

		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		byte v_start = Protocol101Struts.VAR_FRAME_START;
		byte length = (byte) buf0.limit();
		byte v_end = Protocol101Struts.VAR_FRAME_END;
		buf1.put(v_start).put(length).put(length).put(v_start).put(buf0).put(
				crcSum).put(v_end).flip();
		return buf1;
	}

	/**
	 * 写入参数
	 * 
	 * @param type
	 *            1-101协议参数、2-硬件参数、3-GPRS配置参数、 4-版本信息、5-101点位信息
	 * @param content
	 * @return 链路地址获取，链路地址长度，传送原因长度，信息体长度
	 */
	public static IoBuffer writeParam(int type, IoBuffer content,byte current) {
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		byte controlCode = 0x53;
		//TODO 判断是否初始化
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		byte[] addrBytes = HexStrUtil.hexStr2Bytes(linkAddrStr);
		// 校验长度，返回复合properties文件长度大小的byte[]
		addrBytes = checkLength(addrBytes, addrLength, true);

		byte dataType = Protocol101Struts.DATA_TYPE_PARAM_SELF_CMD;
		byte vsqB = 0x1;
		byte[] transCauses = new byte[transCausesLength];
		byte transCause = 0x10;
		transCauses[0] = transCause;

		byte[] infoObjAddr = new byte[2];
		switch (type) {
		case 1: {
			infoObjAddr = Protocol101Struts.protocolParamAddr;
			 infoObjAddr = turnBytes(infoObjAddr);
			 buf0.put(controlCode).put(addrBytes).put(dataType).put(vsqB).put(transCauses).put(addrBytes).put(infoObjAddr).put(content).flip();
			break;
		}

		case 2: {
			infoObjAddr = Protocol101Struts.hardParamAddr;
			 infoObjAddr = turnBytes(infoObjAddr);
			 buf0.put(controlCode).put(addrBytes).put(dataType).put(vsqB).put(transCauses).put(addrBytes).put(infoObjAddr).put(content).flip();
			break;
		}

		case 3: {
			infoObjAddr = Protocol101Struts.grpsParamAddr;
			infoObjAddr = turnBytes(infoObjAddr);
			buf0.put(controlCode).put(addrBytes).put(dataType).put(vsqB).put(transCauses).put(addrBytes).put(infoObjAddr).put(content).flip();
			break;
		}

		case 4: {
			break;
		}

		case 5: {
			vsqB = (byte) (content.limit()/4);
			//TODO
			switch (1) {
			case 1:
				infoObjAddr[0] = Protocol101Struts.pointDigit[0];
				infoObjAddr[1] = Protocol101Struts.pointDigit[1];
				break;
			case 2:
				infoObjAddr[0] = Protocol101Struts.pointMock[0];
				infoObjAddr[1] = Protocol101Struts.pointMock[1];
				break;
			case 3:
				infoObjAddr[0] = Protocol101Struts.pointSet[0];
				infoObjAddr[1] = Protocol101Struts.pointSet[1];
				break;
			case 4:
				infoObjAddr[0] = Protocol101Struts.pointControl[0];
				infoObjAddr[1] = Protocol101Struts.pointControl[1];
				break;
			default:
				break;
			}
			infoObjAddr[1] = current;
			break;
		}

		default:
			break;
		}


		byte crcSum = AddCrc.getCrc(buf0);

		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		byte v_start = Protocol101Struts.VAR_FRAME_START;
		byte length = (byte) buf0.limit();
		byte v_end = Protocol101Struts.VAR_FRAME_END;
		buf1.put(v_start).put(length).put(length).put(v_start).put(buf0).put(
				crcSum).put(v_end).flip();
		return buf1;
	}

	/**
	 * @param linkAddr
	 * @return
	 */
	public static IoBuffer allCalls(byte[] linkAddr) {
		try {

			// TODO 深圳科陆总召控制域
			byte controlByte = (byte) 0x53;
			// 校验长度，返回复合properties文件长度大小的byte[]
			linkAddr = checkLength(linkAddr, addrLength, true);

			byte typeSign = Protocol101Struts.ALL_CALL_TYPE_100;
			byte vsq = 0x1;
			byte[] transCauses = new byte[transCausesLength];
			transCauses[0] = Protocol101Struts.TRANS_CAUSE_ASK_CMD;
			byte[] infoBody = { 0, 0, 0x14 };

			IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
			buf0.put(controlByte).put(linkAddr).put(typeSign).put(vsq).put(
					transCauses).put(linkAddr).put(infoBody).flip();
			byte crc = AddCrc.getCrc(buf0);

			byte length = (byte) buf0.limit();

			IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
			buf1.put(Protocol101Struts.VAR_FRAME_START).put(length).put(length)
					.put(Protocol101Struts.VAR_FRAME_START).put(buf0).put(crc)
					.put(Protocol101Struts.VAR_FRAME_END).flip();

			return buf1;
		} catch (Exception e) {
			// TODO: handle exception
			return IoBuffer.allocate(0);
		}

	}

	/**
	 * 获取同步指令
	 * 
	 * @return 链路地址长度 传送原因长度，
	 */
	public static IoBuffer syncHostCMD(byte[] linkAddrBytes) {
		// 校验长度，返回复合properties文件长度大小的byte[]
		linkAddrBytes = checkLength(linkAddrBytes, addrLength, true);

		byte controlByte = (byte) 0x83;
		byte typeSign = Protocol101Struts.SYNCH_TIME;
		byte vsq = 0x1;
		byte[] transCauses = new byte[transCausesLength];
		transCauses[0] = Protocol101Struts.TRANS_CAUSE_INIT_CMD;
		byte[] infoBody = { 0, 0, 0x14 };
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		buf0.put(controlByte).put(linkAddrBytes).put(typeSign).put(vsq).put(
				transCauses).put(linkAddrBytes).put(infoBody).flip();
		byte crc = AddCrc.getCrc(buf0);
		byte length = (byte) buf0.limit();
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.VAR_FRAME_START).put(length).put(length)
				.put(Protocol101Struts.VAR_FRAME_START).put(buf0).put(crc).put(
						Protocol101Struts.VAR_FRAME_END).flip();
		return buf1;
	}

	/**
	 * 历史遥信激活
	 * 
	 * @return 链路地址获取 链路地址长度 传送原因长度，
	 */
	public static IoBuffer HisDigitalCMD() {
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte controlByte = (byte) 0x73;
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrByte = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		byte typeSign = Protocol101Struts.DATA_TYPE_SINGLE_Q_TIME_DIGTAL;
		byte vsq = 0x01;
		byte[] transCauses = new byte[transCausesLength];
		transCauses[0] = Protocol101Struts.TRANS_CAUSE_ACT_CMD;
		byte[] pointinfo = { 0, 0 };
		byte c = 0x14;
		buffer.put(controlByte).put(linkAddrByte).put(typeSign).put(vsq).put(
				transCauses).put(linkAddrByte).put(pointinfo).put(c).flip();
		byte crc = AddCrc.getCrc(buffer);
		byte length = (byte) buffer.limit();
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.VAR_FRAME_START).put(length).put(length)
				.put(Protocol101Struts.VAR_FRAME_START).put(buffer).put(crc)
				.put(Protocol101Struts.VAR_FRAME_END).flip();
		return buf1;
	}


	/**
	 * 101 历史遥测,历史soe确认报文
	 * 
	 * @return 链路地址获取 链路地址长度
	 */
	public static IoBuffer sureMsgSuc() {
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrByte = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		buffer.put(Protocol101Struts.INIT_ANSWER_02).put(linkAddrByte).flip();

		byte crc = AddCrc.getCrc(buffer);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.FIX_FRAME_START).put(buffer).put(crc).put(
				Protocol101Struts.VAR_FRAME_END).flip();
		return buf1;
	}

	/**
	 * 遥信遥测报文修改、读取成功后确认报文。
	 * @return
	 */
	public static IoBuffer surePointWriteOrReadMsg(byte transCause,byte[] pointValue){

		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrByte = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		byte controlByte = (byte) 0x53;
		byte typeSign = (byte) 0x82;
		byte vsq = 0x1;
		byte[] transCauses = new byte[transCausesLength];
		transCauses[0] = transCause;
		byte[] infoBody = { 0, 0};
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		buf0.put(controlByte).put(linkAddrByte).put(typeSign).put(vsq).put(
				transCauses).put(linkAddrByte).put(turnBytes(pointValue)).put(infoBody).flip();
		byte crc = AddCrc.getCrc(buf0);
		byte length = (byte) buf0.limit();
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.VAR_FRAME_START).put(length).put(length)
				.put(Protocol101Struts.VAR_FRAME_START).put(buf0).put(crc).put(
						Protocol101Struts.VAR_FRAME_END).flip();
		return buf1;
	}
	
	public static byte[] turnBytes(byte[] source) {
		if (source == null) {
			return null;
		}
		int size = source.length;
		byte[] reBytes = new byte[size];
		for (int i = 0; i < size; i++) {
			reBytes[i] = source[i];
		}
		byte temp = 0;
		for (int i = 0; i < size / 2; i++) {
			temp = reBytes[i];
			reBytes[i] = reBytes[size - 1 - i];
			reBytes[size - 1 - i] = temp;
		}
		return reBytes;
	}

	/**
	 * 请求链路 主→设
	 * 
	 * @return 链路地址获取 链路地址长度
	 */
	public static IoBuffer hostAskLink() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.INIT_HOST_01).put(linkAddrBytes).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 *复位远方链路 主→设
	 * 
	 * @return 链路地址获取 链路地址长度
	 */
	public static IoBuffer hostResetLink() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.INIT_HOST_02).put(linkAddrBytes).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 *链路确认 主→设
	 * 
	 * @return 链路地址获取 链路地址长度
	 */
	public static IoBuffer hostLinkConfirm() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.INIT_ANSWER_03).put(linkAddrBytes).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 *复位确认 主→设
	 * 
	 * @return链路地址获取 链路地址长度
	 */
	public static IoBuffer hostResetConfirm() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.INIT_ANSWER_04).put(linkAddrBytes).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 *结束确认 主→设
	 * 
	 * @return链路地址获取 链路地址长度
	 */
	public static IoBuffer endConfirm() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.INIT_ANSWER_END).put(linkAddrBytes).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}

	/**
	 * 时钟同步命令(激活) 主→设
	 * 
	 * @return链路地址获取 链路地址长度
	 */
	public static IoBuffer synchTime() {
		//TODO ready
		// 校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes = checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr),
				addrLength, true);

		byte controlByte = (byte) 0xF3;
		byte typeSign = Protocol101Struts.SYNCH_TIME;
		byte vsq = 0x1;
		byte[] transCauses = new byte[1];
		transCauses[0] = Protocol101Struts.TRANS_CAUSE_ACT_CMD;
		byte[] pointValue = { 0, 0 };
		byte[] syncTimeBytes = DateUtil101V2.getDateBytes(new Date());
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		buf0.put(controlByte).put(linkAddrBytes).put(typeSign).put(vsq).put(
				transCauses).put(linkAddrBytes).put(pointValue).put(
				syncTimeBytes).flip();
		byte crc = AddCrc.getCrc(buf0);
		byte length = (byte) buf0.limit();
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(Protocol101Struts.VAR_FRAME_START).put(length).put(length)
				.put(Protocol101Struts.VAR_FRAME_START).put(buf0).put(crc).put(
						Protocol101Struts.VAR_FRAME_END).flip();
		return buf1;
	}

	/**
	 * 时钟同步命令(激活确认)
	 * 
	 * @return
	 */
	public static IoBuffer synchTimeConfirm() {
		// 短帧数据：
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//TODO ready
		/*if (!HostInfoState.isReady()) {
			return null;
		}*/
		//校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes  =checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr), addrLength, true);
		
		byte[] timer = DateUtil101V2.getDateBytes(new Date());
		buf0.put(Protocol101Struts.FIX_FRAME_START).put(
				Protocol101Struts.SYNCH_TIME_CONFIRM_02).put(linkAddrBytes)
				.put(timer).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}


	/**
	 * 遥控指令
	 * @param pointBytes-遥控点号
	 * @param value-遥控值
	 * @return
	 */
	public static IoBuffer controlDeviceCMD(byte[] pointBytes,byte value){
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//控制域
		byte controlByte = 0xa;
		//数据类型：单点遥控
		byte dataType = 0x2d;
		byte vsq = 0x1;
		//校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes  =checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr), addrLength, true);
		
		byte[] transCause = new byte[transCausesLength];
		transCause[0] = 0x6;
		buf0.put(controlByte).put(linkAddrBytes).put(dataType).put(vsq).put(transCause)
			.put(linkAddrBytes).put(pointBytes).put(value).flip();
		
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}
	
	/**
	 * 主机遥测数据召唤
	 * @return
	 */
	public static IoBuffer callHostMockDataCMD(){
		IoBuffer buf0 = IoBuffer.allocate(0).setAutoExpand(true);
		//控制域
		byte controlByte = 0xa;
		//数据类型：单点遥控
		byte dataType = 0xb;
		byte vsq = 0x1;
		byte[] contents = {0,0,0x5a};
		//校验长度，返回复合properties文件长度大小的byte[]
		byte[] linkAddrBytes  =checkLength(HexStrUtil.hexStr2Bytes(linkAddrStr), addrLength, true);
		
		byte[] transCause = new byte[transCausesLength];
		transCause[0] = 0x5a;
		buf0.put(controlByte).put(linkAddrBytes).put(dataType).put(vsq).put(transCause)
			.put(linkAddrBytes).put(contents).flip();
		byte crcSum = AddCrc.getCrc(buf0);
		IoBuffer buf1 = IoBuffer.allocate(0).setAutoExpand(true);
		buf1.put(buf0).put(crcSum).put(Protocol101Struts.FIX_FRAME_END).flip();
		return buf1;
	}
	
	/**
	 * @param checkBytes
	 * @param length
	 *            转换后的长度
	 * @param isReverse
	 *            是否反转
	 * @return
	 */
	public static byte[] checkLength(byte[] checkBytes, int length,
			boolean isReverse) {
		byte[] infoObj = new byte[length];
		if (checkBytes.length >= length) {
			for (int i = 0; i < length; i++) {
				infoObj[i] = checkBytes[i];
			}
		} else {
			for (int i = 0; i < checkBytes.length; i++) {
				infoObj[length - 1 - i] = checkBytes[checkBytes.length - 1 - i];
			}
		}
		if (isReverse) {
			infoObj = turnBytes(infoObj);
		}
		return infoObj;
	}
}
